<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Member\User;
use App\Models\Member\Mean;
use App\Common\CodeHandler;
use App\Common\WechatHandler;
use Auth;

class UserController extends Controller
{
    public function passwordLogin(Request $request)
    {
        $storeId = $request['storeId'];
        $phone = $request['phone'];
        $password = $request['password'];

        $user = new User;
        $data = $user->passwordLogin($storeId, $phone, $password);
        if (!$data['res'])
            return self::errorClient([], $data['info']);

        return self::successClient($data['token']);
    }

    public function sendCode(Request $request)
    {
        $storeId = $request['storeId'];
        $phone = $request['phone'];
        $handlerType = $request['handlerType'];

        $result = CodeHandler::makeCode($storeId, $phone, $handlerType);
        if (!$result['res'])
            return self::errorClient([], $result['info']);

        return self::successClient([], "发送成功");
    }

    public function codeLogin(Request $request)
    {
        $storeId = $request['storeId'];
        $phone = $request['phone'];
        $code = $request['code'];

        $user = new User;
        $data = $user->codeLogin($storeId, $phone, $code);
        if (!$data['res'])
            return self::errorClient([], $data['info']);

        return self::successClient($data['token']);
    }

    public function getUserInfo(Request $request)
    {
        $user = new User;
        $user = $user->getUserInfo();

        return self::successClient($user, '用户数据获取成功');
    }

    public function bind(Request $request){
        $storeId = $request['storeId'];
        $pid = $request['pid'];
        $code = $request['code'];

        $data = WechatHandler::wxLogin('', $storeId, 'bind', $code, $pid);
        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }
        return self::successClient($data);
    }

    // 判断支付密码是否设置
    public function isPayPassword(Request $request)
    {
        $user = Auth::user()->toArray();
        $Users = new User;
        $payPassword = $Users->isPayPassword($user['id']);
        if (empty($payPassword['password_pay'])) {
            return $this->errorClient(0, '支付密码未设置');
        } else {
            return $this->successClient(1, '支付密码已经设置!'); 
        }
    }

    // 设置支付密码
    public function setPayPassword(Request $request)
    {
        $user = Auth::user()->toArray();
        $Users = new User;
        $passwordPay = $request['password_pay'];
        // $passwordPay = 123456;
        $payPassword = $Users->setPayPassword($user['id'], $passwordPay);
        return $payPassword;
    }

    public function getFansList(Request $request)
    {
        $user = new User;
        $data = $user->getFansList($request);

        return self::successClient($data);
    }

    public function getFansDetail(Request $request)
    {
        $fansId = $request['id'];
        $user = new User;
        $data = $user->getFansDetail($fansId);

        return self::successClient($data);
    }

    public function fansManage(Request $request)
    {
        $user = new User;
        $data = $user->fansManage();

        return self::successClient($data);
    }

    public function setNickname(Request $request)
    {
        $nickname = $request['nickname'];
        $user = new User;
        $data = $user->updateNickname($nickname);
        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }

        return self::successClient($data);
    }

    public function setName(Request $request)
    {
        $name = $request['name'];
        $idCard = $request['idCard'];
        $user = new User;
        $data = $user->updateName($name,$idCard);
        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }

        return self::successClient($data);
    }

    public function setPhone(Request $request)
    {
        $code = $request['code'];
        $phone = $request['phone'];
        $user = new User;
        $data = $user->setPhone($code, $phone);
        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }

        return self::successClient($data);
    }

    public function setWechat(Request $request)
    {
        $wechat = $request['wechat'];
        $user = new User;
        $data = $user->setWechat($wechat);

        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }
        return self::successClient($data);
    }

    public function recommend()
    {
        $user = new User;
        $data = $user->recommend();

        return self::successClient($data);
    }

    public function setPassword(Request $request)
    {
        $user = new User;
        $data = $user->setPassword($request);

        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }
        return self::successClient($data);
    }

    public function setPasswordByOrigin(Request $request) {
        $user = new User;
        $data = $user->setPasswordByOrigin($request);

        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }
        return self::successClient($data);
    }

    public function setAvatar(Request $request)
    {
        $user = new User;
        $data = $user->setAvatar($request);

        if (!$data['res']) {
            return self::errorClient([], $data['info']);
        }
        return self::successClient($data);
    }

    public function getParentInfo(Request $request)
    {
        $user = new User;
        $data = $user->getParentInfo($request['pid']);

        return self::successClient($data);
    }

    public function getStoreInfo(Request $request)
    {
        $user = new User;
        $data = $user->getStoreInfo($request['storeId']);

        return self::successClient($data);
    }

    //微信菜单 公司相关资料列表
    public function getMeanList(Request $request){
        $mean = new Mean;
        $data = $mean::where('status', 1)->orderBy('sort', 'desc')->orderBy('created_at', 'desc')->paginate(10)->toArray();
        return self::successClient($data);
    }
}
